package servlet.MaterialServlet;

import dao.MaterialDao;
import entity.MaterialBean;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@WebServlet("/MaterialListServlet")
public class MaterialListServlet extends HttpServlet {
    public MaterialListServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        ArrayList<String> itemClassesList = new ArrayList<>();
        String itemId = "";
        String itemName = "";
        String itemClasses = "";
        String vendorName = "";
        String site = "";
        String inboundTime = "";
        String contact = "";
        String phoneNumber = "";
        String itemDescription = "";
        int searchFlag=0;
        if(request.getParameter("searchFlag")!=null) {
            searchFlag = Integer.parseInt(request.getParameter("searchFlag"));
            if (request.getParameter("itemId") != null) {
                itemId = request.getParameter("itemId");
                System.out.println("aixiong");
            }
            if (request.getParameter("itemName") != null) itemName = request.getParameter("itemName");
            if (request.getParameter("itemClasses") != null) itemClasses = request.getParameter("itemClasses");
            if (itemClasses.contains(",")) {
                String[] itemClassesArray = itemClasses.split(","); // 使用逗号作为分隔符进行拆分
                Collections.addAll(itemClassesList, itemClassesArray); // 将数组中的所有元素添加到列表中
            } else {
                itemClassesList.add(itemClasses);
            }

            if (request.getParameter("vendorName") != null) vendorName = request.getParameter("vendorName");
            if (request.getParameter("site") != null) site = request.getParameter("site");
            if (request.getParameter("inboundTime") != null) inboundTime = request.getParameter("inboundTime");
            if (request.getParameter("contact") != null) contact = request.getParameter("contact");
            if (request.getParameter("phoneNumber") != null) phoneNumber = request.getParameter("phoneNumber");
            if (request.getParameter("itemDescription") != null)
                itemDescription = request.getParameter("itemDescription");
        }
        MaterialDao materialDao = new MaterialDao();
        int pageCount = materialDao.findPageCount(itemId,  itemName,  itemClassesList,  vendorName,  site,  inboundTime,  contact,  phoneNumber,  itemDescription);
        int curpage = 1;
        if (request.getParameter("page") != null) {
            String pg = request.getParameter("page");
            curpage = Integer.parseInt(pg);
        }
        int al = 0; //al为-1时表示上一页，为1时表示下一页，默认为0
        if (request.getParameter("alterpage") != null) {
            String alpg = request.getParameter("alterpage");
            al = Integer.parseInt(alpg);
        }
        if (curpage > 1 || al > 0) {
            curpage += al;
        }
        if (curpage > pageCount) {
            curpage = pageCount;
        }
//        if(request.getParameter("id")!=null){
//            strProdNo = Integer.parseInt(req.getParameter("prod_no"));
//        }
        MaterialDao dao = new MaterialDao();
        ArrayList<MaterialBean> materialBeanList = null;

        System.out.println("aixiong:"+itemId);
        materialBeanList = dao.findMaterial(itemId,itemName,itemClassesList,vendorName,site,inboundTime,contact,phoneNumber,itemDescription,curpage);
        request.setAttribute("itemId", itemId);
        request.setAttribute("itemName", itemName);
        request.setAttribute("itemClassesList", itemClassesList);
        request.setAttribute("vendorName", vendorName);
        request.setAttribute("phoneNumber", phoneNumber);
        request.setAttribute("pageCount", pageCount);
        request.setAttribute("curpage", curpage);
        request.setAttribute("list", materialBeanList);
        request.setAttribute("searchFlag", searchFlag);
        request.getRequestDispatcher("material_list.jsp").forward(request, response);
    }
}
